package com.zto.sch.base.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-01-14 15:06
 **/
public class ThreadPrint100 {
    private static int count = 1;


    static Thread oddThread = null;
    static Thread evenThread = null;

    public static void main(String[] args) {
        oddThread = new Thread("oddThread1") {
            @Override
            public void run() {
                while (count < 1001) {
                    if (count % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + "----------" + count);
                        count++;
                        LockSupport.unpark(evenThread);
                    } else {
                        LockSupport.park();
                    }
                }
            }
        };
        evenThread = new Thread("evenThread2") {
            @Override
            public void run() {
                while (count < 1001) {
                    if (count % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + "---" + count);
                        count++;
                        LockSupport.unpark(oddThread);
                    } else {
                        LockSupport.park();
                    }
                }
            }
        };

        oddThread.start();
        evenThread.start();

    }
}
